package com.changhong.wlyspectrum.ForgetpsdActivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.changhong.wlyspectrum.NetWork.HttpMethods;
import com.changhong.wlyspectrum.R;
import com.changhong.wlyspectrum.utils.MyConfig;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by Mr_tao
 * on 2017/3/7.
 */
public class ForgetpsdActivity extends Activity{
    private final static String TAG = "ForgetpsdActivity";
    private ImageView mBackImv;
    private EditText mPhoneNumEdt;
    private EditText mNewPsdEdt;
    private EditText mVerifyCodeEdt;
    private Button mGetCodeBtn;
    private Button mNextBtn;
    private String mPhoneNum;
    private String mNewPsd;
    private String mVerifyCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpsd_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar_forgetpsd);

        InitUi();
    }

    private void InitUi(){
        mBackImv = (ImageView)findViewById(R.id.forget2login);
        mBackImv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mPhoneNumEdt = (EditText)findViewById(R.id.psd_phoneNum);
        mPhoneNumEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    mPhoneNumEdt.setHint(null);
                }else{
                    if(mPhoneNumEdt.getText().length() != 0){
                        mPhoneNumEdt.setHint(null);
                    }else{
                        mPhoneNumEdt.setHint(R.string.input_phone);
                    }
                }

            }
        });
        mPhoneNumEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(mPhoneNumEdt.getText().length() != 0){
                    mPhoneNumEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mNewPsdEdt = (EditText)findViewById(R.id.psd_new);
        mNewPsdEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    mNewPsdEdt.setHint(null);
                }else{
                    if(mNewPsdEdt.getText().length() != 0){
                        mNewPsdEdt.setHint(null);
                    }else{
                        mNewPsdEdt.setHint(R.string.input_new_pwd);
                    }
                }

            }
        });
        mNewPsdEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(mNewPsdEdt.getText().length() != 0){
                    mNewPsdEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mVerifyCodeEdt = (EditText)findViewById(R.id.psd_verifyCode);
        mVerifyCodeEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    mVerifyCodeEdt.setHint(null);
                }else{
                    if(mVerifyCodeEdt.getText().length() != 0){
                        mVerifyCodeEdt.setHint(null);
                    }else{
                        mVerifyCodeEdt.setHint(R.string.input_verify);
                    }
                }

            }
        });
        mVerifyCodeEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(mVerifyCodeEdt.getText().length() != 0){
                    mVerifyCodeEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mGetCodeBtn = (Button)findViewById(R.id.psd_verify_btn);
        mGetCodeBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPhoneNum = mPhoneNumEdt.getText().toString();
                if (MyConfig.isMobile(mPhoneNum)) {
                    Log.e(TAG, "get verify code");
                    GetCode(mPhoneNum);
                    mGetCodeBtn.setEnabled(false);
                } else {
                    Toast.makeText(getApplicationContext(), R.string.input_correct_phone, Toast.LENGTH_SHORT).show();
                };
            }
        });

        mNextBtn = (Button)findViewById(R.id.psd_next_btn);
        mNextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPhoneNum = mPhoneNumEdt.getText().toString();
                mNewPsd = mNewPsdEdt.getText().toString();
                mVerifyCode = mVerifyCodeEdt.getText().toString();

                if(!mPhoneNum.isEmpty()){
                    if(!mNewPsd.isEmpty()){
                        if(!mVerifyCode.isEmpty()){
                            VerifyCode(mPhoneNum, mVerifyCode);
                        }else{
                            Toast.makeText(getApplicationContext(),
                                    R.string.input_verify, Toast.LENGTH_LONG).show();
                        }
                    }else{
                        Toast.makeText(getApplicationContext(),
                                R.string.input_new_pwd, Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(),
                            R.string.input_phone, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private Handler forgotHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getApplicationContext(),
                            R.string.send_message, Toast.LENGTH_LONG).show();
                    mGetCodeBtn.setEnabled(true);
                    break;
                case 1:
                    Log.e(TAG, "verify code ok");
                    //new FindPwdThread(mPhoneNum, mVerifyCode, mNewPsd).start();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(),
                            R.string.verify_error, Toast.LENGTH_LONG).show();
                    break;
                case 3:
                    Toast.makeText(getApplicationContext(),
                            R.string.psd_find_success, Toast.LENGTH_LONG).show();
                    finish();
                    break;
                case 4:
                    Toast.makeText(getApplicationContext(),
                            R.string.psd_find_fail, Toast.LENGTH_LONG).show();
                    break;
                case 98:
                    Toast.makeText(getApplicationContext(),
                            R.string.verify_fail, Toast.LENGTH_LONG).show();
                    mGetCodeBtn.setEnabled(true);
                    break;
                case 99:
                    Toast.makeText(getApplicationContext(),
                            R.string.network_error, Toast.LENGTH_LONG).show();
                    mGetCodeBtn.setEnabled(true);
                    break;
                default:
                    break;
            }
        }
    };

    private void GetCode(final String phonenum){
        new Thread(){
            @Override
            public void run() {
                super.run();
                JSONObject js = new JSONObject();
                try {
                    js.put("phone",phonenum);
                    Response response = HttpMethods.GetResponse("/account/register/step1",js.toString());
                    if(response.isSuccessful()){
                        String result = response.body().string();
                        Log.e(TAG,result);
                        JSONObject jsonObject = new JSONObject(result);
                        String code = jsonObject.getString("code");
                        if(code.compareTo("0") == 0){
                            forgotHandler.sendMessage(Message.obtain(forgotHandler, 0));
                        }else{
                            forgotHandler.sendMessage(Message.obtain(forgotHandler, 98));
                        }
                    }else{
                        forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    }
                } catch (JSONException e) {
                    forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    e.printStackTrace();
                } catch (IOException e) {
                    forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void VerifyCode(final String phonenum, final String verifycode){
        new Thread(){
            @Override
            public void run() {
                super.run();
                JSONObject js = new JSONObject();
                try{
                    js.put("phone",phonenum);
                    js.put("verifyCode",verifycode);
                    Response response = HttpMethods.GetResponse("/account/register/step2",js.toString());
                    if(response.isSuccessful()){
                        String result = response.body().string();
                        Log.e(TAG,result);
                        JSONObject jsonObject = new JSONObject(result);
                        String code = jsonObject.getString("code");
                        if(code.compareTo("0") == 0){
                            forgotHandler.sendMessage(Message.obtain(forgotHandler, 1));
                        }else{
                            forgotHandler.sendMessage(Message.obtain(forgotHandler, 2));
                        }
                    }else{
                        forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    }
                } catch (JSONException e) {
                    forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    e.printStackTrace();
                } catch (IOException e) {
                    forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void ResetPsd(final String phonenum, final String verifycode, final String newpsd){
        new Thread(){
            @Override
            public void run() {
                super.run();
                JSONObject js = new JSONObject();
                try{
                    js.put("phone",phonenum);
                    js.put("verifyCode",verifycode);
                    js.put("newPwd",newpsd);
                    Response response = HttpMethods.GetResponse("/account/findpwd",js.toString());
                    if(response.isSuccessful()){
                        String result = response.body().string();
                        Log.e(TAG,result);
                        JSONObject jsonObject = new JSONObject(result);
                        String code = jsonObject.getString("code");
                        if(code.compareTo("0") == 0){
                            forgotHandler.sendMessage(Message.obtain(forgotHandler, 3));
                        }else{
                            forgotHandler.sendMessage(Message.obtain(forgotHandler, 4));
                        }
                    }else{
                        forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    }
                } catch (JSONException e) {
                    forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    e.printStackTrace();
                } catch (IOException e) {
                    forgotHandler.sendMessage(Message.obtain(forgotHandler, 99));
                    e.printStackTrace();
                }
            }
        }.start();

    }
}
